前面幾天我們嘗試了 Dockerfile 跟 Docker Compose 去實現我們所設計的系統,你實作會發現這兩個其實相輔相成,可能在設定、管理、執行這兩個都有適合的用處,就看大家自己如何去分配
我們先來一一講解 docker-compose.yml 的內容
version: "3.8"
services:
mysql-server:
build:
context: ./mysql
image: mysql-image:latest
container_name: mysql-contianer
environment:
- MYSQL_DATABASE=ItDB
- MYSQL_USER=ITUser
- MYSQL_PASSWORD="Mysql Password"
- MYSQL_ROOT_PASSWORD="Mysql Root Password"
volumes:
- mysqlVolumes:/var/log
networks:
- composeBridge
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
interval: 15s
timeout: 5s
retries: 3
redis-server:
build:
context: ./redis
image: redis-image:latest
container_name: redis-container
command: redis-server /usr/local/etc/redis/redis.conf
depends_on:
- mysql-server
networks:
- composeBridge
django-python-server:
build:
context: ./DjangoWeb
image: django-python-image:latest
container_name: django-python-container
depends_on:
mysql-server:
condition: service_healthy
networks:
- composeBridge
nginx-server:
build:
context: ./nginx
image: nginx-image:latest
container_name: nginx-container
ports:
- "127.0.0.1:8003:80"
depends_on:
- django-python-server
networks:
- composeBridge
volumes:
mysqlVolumes:
name: mysql_volumes
networks:
composeBridge:
name: compose_bridge
driver: bridge
如果我們Dockerfile 跟 Docker Compose 並用會發現效率會好多
docker-compose up -d
指令 直接建立 Image 並且啟動 Containerdocker-compose up -d
指令,就會針對你修改的Service重新啟動Containerdocker-compose up -d
指令,就會重新建立Image並且起重新啟動Container真的要自己實作Dockerfile 跟 Docker Compose才會感覺差別在哪,而且也知道如何管理相關docker的資料